package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import dao.AdminDao;
import dao.ParamsDao;
import entity.Admin;
import entity.AdminPrivs;

public class SearchAdminServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2489354851592963827L;
	List<Map<String, String>> list = null;
	String adminName;
	String deptName;
	String priv;
	String status;
	
	public SearchAdminServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		adminName = request.getParameter("adminName");
		deptName = URLDecoder.decode(URLDecoder.decode(request.getParameter("deptName"), "UTF-8"), "UTF-8");
		priv = URLDecoder.decode(URLDecoder.decode(request.getParameter("priv"), "UTF-8"), "UTF-8");
		status = URLDecoder.decode(URLDecoder.decode(request.getParameter("status"), "UTF-8"), "UTF-8");
		AdminDao adminDao = null;
		AdminPrivs adminPrivs = null;
		StringBuffer privsJson = null;
		Set<String> privsSet = null;
		List<Admin> admins = null;
		
		try {
			adminDao = new AdminDao();
			setCondition();
			admins = adminDao.queryAdmins(list);
			privsJson = new StringBuffer("{\"admins\": [");
			for(Admin admin : admins) {// 一个admin一条记录
				adminPrivs = new AdminPrivs();
				adminPrivs = adminDao.queryAdminPrivs(admin.getAdminName());
				privsSet = adminPrivs.getPrivNameAddUser().keySet();
				privsJson.append("{");
				privsJson.append("\"adminName\": \"").append(admin.getAdminName()).append("\",");
				privsJson.append("\"privs\": \"");
				
				if (privsSet.size() > 0) {
					for(String priv : privsSet) {// 一条记录多个权限
						privsJson.append(priv).append("、");
					}
					privsJson.deleteCharAt(privsJson.length() - 1);
				}
				
				privsJson.append("\",");
				privsJson.append("");// TODO：分配用户激活
				privsJson.append("\"status\":\"").append(admin.getStatus()).append("\",");
				privsJson.append("\"deptName\":\"").append(admin.getDeptName()).append("\",");
				privsJson.append("\"phone\":\"").append(admin.getPhone()).append("\",");
				privsJson.append("\"isSuper\":\"").append(admin.getIsSuper()).append("\",");
				privsJson.append("\"password\":\"").append(admin.getPassword()).append("\",");
				privsJson.append("\"email\":\"").append(admin.getEmail()).append("\",");
				privsJson.append("\"cnName\":\"").append(admin.getCnName()).append("\",");
				privsJson.append("\"createDate\": \"").append(admin.getCreateDate().toString()).append("\"");
				privsJson.append("},");
			}
			privsJson.deleteCharAt(privsJson.length() - 1).append("]}");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			out.print(privsJson.toString());
			out.close();
		}
	}
	
	// 封装查询条件
	private void setCondition() {
		list = new ArrayList<Map<String, String>>();
		if(!StringUtils.isEmpty(adminName)) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "adminName");
			map.put("值", adminName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(deptName)) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "deptName");
			map.put("值", deptName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(priv)) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "privName");
			map.put("值", priv);
			list.add(map);
		}
		if(!StringUtils.isEmpty(status)) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "status");
			map.put("值", status);
			list.add(map);
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	public void init() throws ServletException {
	}

}
